<template>
  <div class="head_menu-wrapper">
    <img class="btn-icon" src="@/assets/header_btn.gif" />
    <div :class="['i-menu',{'i-r-menu':menu.class}]" :key="index" v-for="(menu,index) in menus">
      <mivaLink :to="menu.to">
        <div :class="['iconfont', menu.icon, menu.class]" :title="menu.num">
          <template v-if="!(menu.icon || menu.class)">{{menu.num | maxNum}}</template>
        </div>
        <div>{{menu.name}}</div>
      </mivaLink>
      <ul class="h-panel">
        <template v-for="(menuChild,indexChild) in menu.child">
          <li :key="indexChild" class="menu_child-group">
            <mivaLink :to="menuChild.to" class="menu_child">
              <span>{{menuChild.name}}</span>
            </mivaLink>
          </li>
        </template>
      </ul>
    </div>
    <navUserCenter :show="accountShow" class="menu-nav-user-center" />
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import navUserCenter from '@/components/nav-user-center/index.vue';


export default {
    components: {
        navUserCenter,
    },
    filters: {
        maxNum(num) {
            if (Number(num) >= 1000) {
                return '999+';
            }
            return Number(num) || '--';
        },
    },
    props: {
        accountShow: Boolean,
    },
    computed: {
        ...mapState('term', ['headMenuData']),
        menus() {
            return this.headMenuData;
        },
    },
    created() {
        this.getHeadMenuAction();
    },
    methods: {
        ...mapActions('term', ['getHeadMenuAction']),
    },
};
</script>


<style lang="less" scoped>
@i-menu-margin: 8px;
@h-panel-path: 0px;

.btn-icon {
  float: right;
  cursor: none;
  user-select: none;
  pointer-events: none;
}

.menu-nav-user-center {
  float: right;
  margin-top: 5px;
  margin-right: 20px;
}

.head_menu-wrapper {
  height: @headMenuHeight;
  position: relative;
  color: @global_text_color;
  user-select: none;
  .i-menu {
    width: @headMenuHeight;
    height: @headMenuHeight;
    margin-right: @i-menu-margin;
    position: relative;
    float: left;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 12px;
    &:first-child .iconfont {
      transform: scale(1.3, 1.3) translateY(-3px);
      font-weight: bold;
      background-color: transparent;
      color: @base_color;
    }
  }
  .iconfont {
    max-width: 28px;
    height: 12px;
    padding: 1px 3px;
    line-height: 14px;
    display: inline-block;
    transform: scale(0.85);
    border-radius: 3px;
    font-size: 12px;
    font-family: sans-serif, sans-serifsans-serif, Calibri, Arial, Helvetica;
    vertical-align: top;
    box-sizing: initial;
    background-color: @base_color;
    color: #fff;
  }
  .h-panel {
    display: none;
    position: absolute;
    top: @headMenuHeight;
    left: @h-panel-path;
    z-index: 1;
    border-top: 0;
    border-radius: 0 0 4px 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.16);
    background: #fff;
    overflow: hidden;
  }
  .i-r-menu {
    float: right;
    margin: 0 0 0 @i-menu-margin;
    .h-panel {
      left: initial;
      right: @h-panel-path;
    }
  }
  .menu_child-group {
    min-width: 120px;
    height: auto;
    line-height: 20px;
    position: relative;
    font-size: 12px;
    text-align: left;
    transition: all 0.2s;
    overflow: hidden;
  }
  .i-menu:hover .h-panel {
    display: block;
    animation: itemHoverPanelShow 0.3s forwards;
  }
  .menu_child {
    display: block;
    padding: 5px 15px 5px 25px;
    margin-right: 10px;
    background: url(~@/assets/icons2.png) no-repeat 12px -1613px;
    white-space: nowrap;
    transition: all 0.2s;
    overflow: hidden;
    position: relative;
    left: 0;
    color: #222;
    span::after {
      content: "";
      background: url(~@/assets/icons2.png) no-repeat 0 -1581px;
      width: 15px;
      height: 18px;
      display: block;
      position: absolute;
      right: -100px;
      top: -3px;
      transition: all 0.2s;
      opacity: 0;
    }
  }
  .menu_child-group:hover {
    background-color: fade(lighten(@base_color, 3%), 55.7%);
    .menu_child {
      transform: translateX(10px);
      span::after {
        opacity: 1;
        right: -21px;
      }
    }
  }
  .item-blackroom {
    width: 18px;
    height: 18px;
    display: block;
    margin: -4px auto 0px;
    background: url(~@/assets/icons.png) no-repeat;
    background-position: -84px -1942px;
  }
  .item-friend {
    width: 18px;
    height: 18px;
    display: block;
    margin: -4px auto 0px;
    background: url(~@/assets/icons.png) no-repeat;
    background-position: -87px -2006px;
  }
}

@keyframes itemHoverPanelShow {
  0% {
    opacity: 0.5;
    transform: translateY(10px);
  }
  50% {
    opacity: 0.8;
    transform: translateY(5px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>


// 数据参照
// "headMenuData": [
//   { "to": "/", "name": "首页", "icon": "icon-shouye1" },
//   {"to":"/audio","name":"音频", "type":2, "num":0},
//   {"to":"/audio","name":"图集", "type":3, "num":0},
//   {"to":"/article","name":"文章", "type":4, "num":0},
//   {"to":"/article","name":"游戏", "type":5, "num":0},
//   {"to":"/article","name":"视频", "type":6, "num":0}
// ],
